$(function () {
    var noticeParam = {
        header: $.getUrlQueryValue("header") || '',
        category: $.getUrlQueryValue("category") ,
        startDate: $.getUrlQueryValue("startDate") || '',
        endDate: $.getUrlQueryValue("endDate") || '',
        pageNum: parseInt($.getUrlQueryValue("pageNum")) || 1,
        pageSize: 15
    }
    
    $('#header').val(noticeParam.header || '')
//单点复选框
    $('.radio-span input[value='+ noticeParam.category +']').parent().addClass('checked')

    var daterangepickerOptions = {
        autoUpdateInput: false,
        endDate: new Date(),
        maxDate:new Date(),
        applyClass : 'btn-default',
        locale: {
            cancelLabel: '取消',
            applyLabel: '确认',
            format: 'YYYY-MM-DD',
            separator: '  -  ',
            fromLabel: '从',
            toLabel: '到',
            weekLabel: 'W',
            daysOfWeek:["日","一","二","三","四","五","六"],
            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
        }
    }

    if (noticeParam.startDate && noticeParam.endDate) {
        $('#dateInterval').val(
            noticeParam.startDate
            + daterangepickerOptions.locale.separator
            + noticeParam.endDate
        );
    }

    $('#dateInterval').daterangepicker(daterangepickerOptions);
    $('#dateInterval').on('apply.daterangepicker', function (ev, picker) {
        noticeParam.startDate = picker.startDate.format('YYYY-MM-DD');
        noticeParam.endDate = picker.endDate.format('YYYY-MM-DD');
        $(this).val(
            noticeParam.startDate
            + daterangepickerOptions.locale.separator
            + noticeParam.endDate
        );
    });
    $('#dateInterval').on('cancel.daterangepicker', function (ev, picker) {
        noticeParam.startDate = '';
        noticeParam.endDate = '';
        $(this).val('');
    });

    $.ajax({
        url: OA_SERVER_NAME+"cxf/noc/getnotice/getgg",
        contentType: 'application/json',
        type:'get',
        dataType: 'json',
        data: {
            "pageNum": noticeParam.pageNum,     // 查询页数
            "header": noticeParam.header,          // 公告标题
            "category": noticeParam.category,      // 公告类型
            "startdate": noticeParam.startDate,    // 开始日期
            "enddate": noticeParam.endDate,        // 结束日期
            //"pageTotal": noticeParam.pageSize,     // 每页总数
            "pageSize": noticeParam.pageSize,     // 每页总数
           // "fbrempName": '',                      // 发布人名
            //"requestSystem": "UAP",                // 请求系统
           // "lmName": '',                          // 栏目ID
            //"empcode": '088936',
            
    	    "empid": '',                         //工号id
            "ggid":''                        //公告id
        },
        success: function (data) {//分页
            if (data) {
            	
                var dataCount = parseInt(data.TotalCount);
                var totalPage = Math.floor((dataCount + noticeParam.pageSize - 1) / noticeParam.pageSize);
                var prePage = noticeParam.pageNum > 1 ? noticeParam.pageNum - 1 : 1
                var nextPage = noticeParam.pageNum < totalPage ? noticeParam.pageNum + 1 : totalPage;
                var isFirst = noticeParam.pageNum <= 1;
                var isLast = noticeParam.pageNum >= totalPage;
                var noticePage = {
                    currentPage: noticeParam.pageNum,
                    dataCount: dataCount,
                    totalPage: totalPage,
                    prePage: prePage,
                    nextPage: nextPage,
                    isFirst: isFirst,
                    isLast: isLast	
                }
                $('#queryNoticeBody').html("");
                for (var i = 0; i < data.reponseParam.length; i++) {
                    var str = '<tr><td>'
                        + '<a target="view_window" href="noticedetail.html?ggid='
                        + data.reponseParam[i].id
                        + '">'
                        + data.reponseParam[i].docsubject
                        + '</a></td><td>'
                        + data.reponseParam[i].docvaliddate
                        +'&nbsp'+'&nbsp'
                        +data.reponseParam[i].docvalidtime
                        + '</td><td>'
                        + data.reponseParam[i].name
                        + '</td><td>'
                        + data.reponseParam[i].departmentname
                        + '</td>'
                    $("#queryNoticeBody").append(str);
                }
                //显示页数数组
                var pageArr = new Array();
                if (totalPage <= 5) { // 总页数
                    for (var i = 1; i <= totalPage; i++) {
                        pageArr.push(i);
                    }
                } else {
                    if (noticeParam.pageNum + 2 <= totalPage && noticeParam.pageNum - 2 > 0) {
                        for (var i = 0; i < 5; i++) {
                            pageArr[i] = noticeParam.pageNum - 2 + i;
                        }
                    } else if (noticeParam.pageNum <= 2) {
                        pageArr = [1, 2, 3, 4, 5];
                    } else {
                        for (var i = 0; i < 5; i++) {
                            pageArr[i] = totalPage - 4 + i;
                        }
                    }
                }

                var $pageContainer = $('#pageContainer');

                // 创建上一页HTML对象
                var prePageHtml =
                    '<li class="paginate_button previous">' +
                    '<a><</a>' +
                    '</li>'
                var $prePage = $(prePageHtml);
                if (noticePage.isFirst) {
                    $prePage.addClass('disabled');
                } else {
                    $prePage.click(function () {
                        noticeParam.pageNum = noticePage.prePage;
                        window.location.href = UAP_SERVER_NAME + 'noticeList.html?' + $.param(noticeParam);
                    });
                }
                $pageContainer.append($prePage);

                // 创建中间部分页码
                var pageNumHtml =
                    '<li class="">' +
                        '<a href="javascript:void(0)">{pageNum}</a>' +
                    '</li>';
                $.each(pageArr, function (index, item) {
                    var $pageNum = $(pageNumHtml.replace('{pageNum}', item));
                    if (noticePage.currentPage == item) {
                        $pageNum.addClass('active');
                    }
                    $pageNum.click(function () {
                        noticeParam.pageNum = item;
                        window.location.href = UAP_SERVER_NAME + 'noticeList.html?' + $.param(noticeParam);
                    });
                    $pageContainer.append($pageNum);
                });

                // 创建下一页HTML对象
                var nextPageHtml =
                    '<li class="paginate_button next">' +
                        '<a>></a>' +
                    '</li>'
                var $nextPage = $(nextPageHtml);
                if (noticePage.isLast) {
                    $nextPage.addClass('disabled');
                } else {
                    $nextPage.click(function () {
                        noticeParam.pageNum = noticePage.nextPage;
                        window.location.href = UAP_SERVER_NAME + 'noticeList.html?' + $.param(noticeParam);
                    });
                }
                $pageContainer.append($nextPage);
            } else {
                alert('获取公告信息失败！')
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown)  {   
        	
        } 

    });
    $('#search').click(function () {
    	
        noticeParam.category = $('.radio-span.checked input').val();
        noticeParam.header = $('#header').val() || '';
        noticeParam.pageNum = 1;
        
        window.location.href = UAP_SERVER_NAME + 'noticeList.html?' + $.param(noticeParam);
    });
   
    //重置后复选框选中页面进来的按钮
   
    var defaultType = $.getUrlQueryValue("catype");
    noticeParam.catype = defaultType;//多次服务器请求保持常量值
    //var value0 = parseInt($.getUrlQueryValue("category"));//分类号，一次获取之后值不再改变
    
    $('#reset').click(function () {
    	
    	$('#dateInterval').val('');
        $('#header').val('');
        
        $('.radio-span input').attr('checked',false);
        
        $('.radio-span').removeClass('checked');
        
        $('.radio-span input[value='+defaultType+']').parent().addClass('checked');
        $('.radio-span input[value='+defaultType+']').attr('checked',true);
        delete  noticeParam.startDate;
        delete  noticeParam.endDate;
        delete  noticeParam.header;
    });

});
